package com.ruoyi.crop.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.crop.api.domain.AroundTeam;
import com.ruoyi.crop.api.domain.ClassAround;
import com.ruoyi.crop.api.domain.PlanAround;
import com.ruoyi.crop.mapper.AroundTeamMapper;
import com.ruoyi.crop.mapper.ClassAroundMapper;
import com.ruoyi.crop.mapper.PlanAroundMapper;
import com.ruoyi.crop.service.ClassAroundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author lenovo
* @description 针对表【src_class_around(班次表)】的数据库操作Service实现
* @createDate 2024-07-28 21:35:07
*/
@Service
public class ClassAroundServiceImpl extends ServiceImpl<ClassAroundMapper, ClassAround>
    implements ClassAroundService{
    @Autowired
    AroundTeamMapper aroundTeamMapper;
    @Autowired
    PlanAroundMapper planAroundMapper;
    @Override
    public List<ClassAround> lists(Integer planId,Integer aroundId) {
        return baseMapper.lists(planId,aroundId);
    }

    @Override
    public void deleteById(String s) {
        ClassAround classAround = baseMapper.selectOne(Wrappers.lambdaQuery(ClassAround.class).eq(ClassAround::getId, s));
        System.err.println(JSONUtil.toJsonStr(classAround));
        PlanAround planAround = planAroundMapper.selectOne(Wrappers.lambdaQuery(PlanAround.class).eq(PlanAround::getPlanId, classAround.getPlanId()));
        System.err.println(JSONUtil.toJsonStr(planAround)+"----------");
        planAroundMapper.delete(Wrappers.lambdaQuery(PlanAround.class).eq(PlanAround::getAroundId, planAround.getAroundId()));
        aroundTeamMapper.delete(Wrappers.lambdaQuery(AroundTeam.class).eq(AroundTeam::getAroundId, planAround.getAroundId()));
        baseMapper.deleteById(s);
    }

    @Override
    public void deleteBatchByIds(List<String> teamId) {
        List<ClassAround> classArounds = baseMapper.selectBatchIds(teamId);
        for (ClassAround classAround : classArounds) {
            aroundTeamMapper.selectOne(Wrappers.lambdaQuery(AroundTeam.class).eq(AroundTeam::getAroundId,classAround.getId()));
        }
        baseMapper.deleteBatchIds(Arrays.asList(teamId));
    }

}




